package com.cwf.business.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cwf.business.R;
import com.cwf.business.view.loading.SpinKitView;
import com.cwf.business.view.loading.Sprite;
import com.cwf.business.view.loading.SpriteFactory;
import com.cwf.business.view.loading.Style;

/**
 * Created by sam_chan on 2017/5/10.
 */
public class ViewUtil {

    /**
     * 自定义Toast
     * @param context
     * @param content
     */
    public static void Toasts(final Activity context, final String content) {
        if (Thread.currentThread().getName().equals("main")) {
            showDiyUIToast(context, content);
        } else {
            context.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    showDiyUIToast(context, content);
                }
            });
        }
    }

    private static void showDiyUIToast(final Activity context, final String content) {
        View view = LayoutInflater.from(context).inflate(R.layout.diytoast,
                null);
        TextView textView = (TextView) view.findViewById(R.id.toast_text);
        textView.setText(content);
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM, 0, 200);
        toast.setView(view);
        toast.show();
    }

    /**
     * 自定义 "请稍等"View
     * @param context
     * @param msg
     * @return
     */
    public static Dialog createLoadingDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.dialog_loading_view);// 加载布局
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        SpinKitView spinKitView = (SpinKitView) v.findViewById(R.id.spin_kit);//等待logo
        Style style = Style.values()[2];
        Sprite drawable = SpriteFactory.create(style);
        spinKitView.setIndeterminateDrawable(drawable);

        Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog
        loadingDialog.setCancelable(true); // 是否可以按“返回键”消失
        loadingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局

        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        loadingDialog.show();

        return loadingDialog;
    }

    /**
     * 关闭dialog
     *
     * http://blog.csdn.net/qq_21376985
     *
     * @param mDialogUtils
     */
    public static void closeDialog(Dialog mDialogUtils) {
        if (mDialogUtils != null && mDialogUtils.isShowing()) {
            mDialogUtils.dismiss();
        }
    }

}
